#!/usr/bin/perl -w

use strict;
use Time::Local;

my $fp;
if ($^O eq "MSWin32") {
    open $fp, "dir /B 201008* |";
} else {
    open $fp, "ls -d 201008* |";
} 
my @dates;
while (<$fp>) {
  chomp;
  push @dates, $_;
}
close $fp;

foreach my $date (sort @dates) {
  if ($^O eq "MSWin32") {
    open $fp, "dir /B $date\\*.csv |";
  } else {
    open $fp, "ls $date/*.csv |";
  }
  while (<$fp>) {
    chomp;
    next if ($_ =~ /[MPV]I_201008/);
    print "reading file $_ ... ";
    my %hash;
    my $symbol;
    if ($^O eq "MSWin32") {
      ($symbol) = $_ =~ /(.*)_$date/;
      open FILE, "$date\\$_" or die $!;
    } else {
      ($symbol) = $_ =~ /^$date\/(.*)_$date/;
      open FILE, "$_" or die $!;
    }
    my $header = <FILE>;
    my $open_int = 0;
    while (<FILE>) {
      chomp;
      my @fields = split /,/, $_;
      my ($year, $mon, $mday) = $fields[0] =~ /(\d{4})-(\d{2})-(\d{2})/;
      my ($hour, $min, $sec) = $fields[1] =~ /(\d+):(\d+):(\d+)/;
      my $time = timelocal($sec, $min, $hour, $mday, $mon - 1, $year - 1900);
      $hash{$time}{last} = $fields[2];
      if (exists $hash{$time}) {
	$hash{$time}{volume} += $fields[3];
      } else {
	$hash{$time}{volume} = $fields[3];
      }
      $hash{$time}{cum_volume} = $fields[4];
      $open_int += $fields[5];
      $hash{$time}{open_int} = $open_int;
      $hash{$time}{bid} = $fields[6];
      $hash{$time}{bid_size} = $fields[7];
      $hash{$time}{ask} = $fields[12];
      $hash{$time}{ask_size} = $fields[13];
    }
    close FILE;
    print "done\n";
    my $output = $symbol.".csv";
    open OUTPUT, ">> $output" or die $!;
    print "writing file $output ... ";
    foreach (sort keys %hash) {
      my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($_);
      $year += 1900;
      $mon += 1;
      print OUTPUT "$year,$mon,$mday,$hour,$min,$sec,$hash{$_}{last},$hash{$_}{volume},$hash{$_}{cum_volume},$hash{$_}{open_int},$hash{$_}{bid},$hash{$_}{bid_size},$hash{$_}{ask},$hash{$_}{ask_size}\n";
    }
    close OUTPUT;
    print "done\n";
  }
  close $fp;
}
